#include <dspSimulator/instruction/cmplt.h>


bool CMPLT::checkCondition(std::shared_ptr<DspMachine> dspMachine) {
    if (isCompactInstruction) {
        return true;
    } else {
        return checkStandardCondition(dspMachine);
    }
}

void CMPLT::loadInstIRIntoJit(llvm::LLVMContext *llvmContext, llvm::Module *module,
                              std::shared_ptr<llvm::IRBuilder<>> irBuilder) {
    llvm::FunctionType *funcType = llvm::FunctionType::get(irBuilder->getVoidTy(), false);
    // --------------------------------- cmplt (.unit) uint,xuint, uint---------------------------------------------
    {
        llvm::Function *cmpltL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "CMPLTKind1",
                                                        module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", cmpltL);
        llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(*llvmContext, "then", cmpltL);
        llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(*llvmContext, "else", cmpltL);
        llvm::BasicBlock *endBB = llvm::BasicBlock::Create(*llvmContext, "end", cmpltL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2 = irBuilder->CreateLoad(src2RegPtr);

        auto src1RegPtr = getSrc1LoRegPtr(module, irBuilder);
        auto src1 = irBuilder->CreateLoad(src1RegPtr);

        auto resPtr = irBuilder->CreateAlloca(irBuilder->getInt32Ty());

        auto cond = irBuilder->CreateICmpSLT(src1, src2);
        irBuilder->CreateCondBr(cond, thenBB, elseBB);

        irBuilder->SetInsertPoint(thenBB);
        irBuilder->CreateStore(irBuilder->getInt32(1), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(elseBB);
        irBuilder->CreateStore(irBuilder->getInt32(0), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(endBB);
        auto res = irBuilder->CreateLoad(resPtr);
        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }

    // --------------------------------- cmplt (.unit) scst5,xuint, uint---------------------------------------------
    {
        llvm::Function *cmpltL = llvm::Function::Create(funcType, llvm::GlobalValue::ExternalLinkage, "CMPLTKind2",
                                                        module);
        llvm::BasicBlock *entryBB = llvm::BasicBlock::Create(*llvmContext, "entry", cmpltL);
        llvm::BasicBlock *thenBB = llvm::BasicBlock::Create(*llvmContext, "then", cmpltL);
        llvm::BasicBlock *elseBB = llvm::BasicBlock::Create(*llvmContext, "else", cmpltL);
        llvm::BasicBlock *endBB = llvm::BasicBlock::Create(*llvmContext, "end", cmpltL);
        irBuilder->SetInsertPoint(entryBB);

        auto dstRegPtr = getDstLoRegPtr(module, irBuilder);

        auto src2RegPtr = getSrc2LoRegPtr(module, irBuilder);
        auto src2 = irBuilder->CreateLoad(src2RegPtr);

        auto cst5 = getCstN(module, irBuilder, 5);
        auto scst = irBuilder->CreateSExt(cst5, irBuilder->getInt32Ty());

        auto resPtr = irBuilder->CreateAlloca(irBuilder->getInt32Ty());

        auto cond = irBuilder->CreateICmpSLT(scst, src2);
        irBuilder->CreateCondBr(cond, thenBB, elseBB);

        irBuilder->SetInsertPoint(thenBB);
        irBuilder->CreateStore(irBuilder->getInt32(1), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(elseBB);
        irBuilder->CreateStore(irBuilder->getInt32(0), resPtr);
        irBuilder->CreateBr(endBB);

        irBuilder->SetInsertPoint(endBB);
        auto res = irBuilder->CreateLoad(resPtr);
        irBuilder->CreateStore(res, dstRegPtr);
        irBuilder->CreateRetVoid();
    }
}

std::shared_ptr<Instruction> CMPLT::decode(std::shared_ptr<InstInfo> instInfo, u64 baseCycle) {
    std::shared_ptr<CMPLT> cmplt;
    auto dspInstInfo = std::dynamic_pointer_cast<DspInstInfo>(instInfo);
    bool isStandardInst = dspInstInfo->isStandardInstruction();
    if (isStandardInst) {
        auto data = vectorToBitSet<32>(dspInstInfo->getBits());
        if (checkBits<10>(data, 2, 0b1010111110) && existStandardCondition(data)) {
            cmplt = std::make_shared<CMPLT>(dspInstInfo->getAddress(), baseCycle);
            cmplt->creg_z = extractBits<4>(data, 28);
            cmplt->dst = extractBits<5>(data, 23).to_ulong();
            cmplt->src2 = extractBits<5>(data, 18).to_ulong();
            cmplt->src1 = extractBits<5>(data, 13).to_ulong();
            cmplt->x = data[12];
            cmplt->s = data[1];
            cmplt->p = data[0];
            if (cmplt->s == 0) {
                cmplt->funcUnit = FuncUnit::L1;
            } else {
                cmplt->funcUnit = FuncUnit::L2;
            }
            cmplt->instFormat = InstFormat::Kind1;
        }
    } else {
        auto data = vectorToBitSet<16>(dspInstInfo->getBits());
        if (data[11] == 1 && checkBits<2>(data, 5, 0b00) && data[10] == 1 && checkBits<3>(data, 1, 0b100)) {
            cmplt = std::make_shared<CMPLT>(dspInstInfo->getAddress(), baseCycle);
            cmplt->isCompactInstruction = true;
            cmplt->compactInstKind = "L2c";
            cmplt->src1 = extractBits<3>(data, 13).to_ulong();
            cmplt->src2 = extractBits<3>(data, 7).to_ulong();
            cmplt->dst = data[4];
            cmplt->s = data[0];
            cmplt->x = data[12];
            if (cmplt->s == 0) {
                cmplt->funcUnit = FuncUnit::L1;
            } else {
                cmplt->funcUnit = FuncUnit::L2;
            }
            cmplt->DSZ = dspInstInfo->getDSZ();
            cmplt->RS = dspInstInfo->getRS();
            cmplt->SAT = dspInstInfo->getSAT();
            cmplt->PROT = dspInstInfo->getPROT();
            cmplt->BR = dspInstInfo->getBR();
            cmplt->p = dspInstInfo->getP();
            cmplt->fixUpRegOnCompactInstruction();
            cmplt->instFormat = InstFormat::Kind1;
        } else if (checkBits<2>(data, 14, 0b00) && data[12] == 1 && data[10] == 0 && checkBits<6>(data, 1, 0b010011)) {
            cmplt = std::make_shared<CMPLT>(dspInstInfo->getAddress(), baseCycle);
            cmplt->isCompactInstruction = true;
            cmplt->compactInstKind = "Lx1c";
            cmplt->cst = data[13];
            cmplt->dst = data[11];
            cmplt->src2 = extractBits<3>(data, 7).to_ulong();
            cmplt->s = data[0];
            if (cmplt->s == 0) {
                cmplt->funcUnit = FuncUnit::L1;
            } else {
                cmplt->funcUnit = FuncUnit::L2;
            }
            cmplt->DSZ = dspInstInfo->getDSZ();
            cmplt->RS = dspInstInfo->getRS();
            cmplt->SAT = dspInstInfo->getSAT();
            cmplt->PROT = dspInstInfo->getPROT();
            cmplt->BR = dspInstInfo->getBR();
            cmplt->p = dspInstInfo->getP();
            cmplt->fixUpRegOnCompactInstruction();
            cmplt->instFormat = InstFormat::Kind2;
        }
    }
    return cmplt;
}

void CMPLT::executeCustom(std::shared_ptr<TargetMachine> targetMachine, ExecutePhase executePhase) {

}

std::string CMPLT::toString() const {
    std::string res;
    if (!isCompactInstruction) {
        res += getStandardConditionString() + " ";
    }
    res += m_name + " " + FuncUnitName[static_cast<u32>(funcUnit)] + " ";
    if (instFormat == InstFormat::Kind1) {
        res += getSrc1Reg32Name() + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    } else if (instFormat == InstFormat::Kind2) {
        res += std::to_string(extractBitsAndToInt<5>(cst)) + "," + getSrc2Reg32Name() + "," + getDstReg32Name();
    }
    return res;
}

std::shared_ptr<DspInstruction> CMPLT::clone() {
    return std::make_shared<CMPLT>(*this);
}
